<template>
  <el-row>
    <el-col :span="8" :offset="14">
      <el-card style="margin-top: 200px">
        <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
          <el-tab-pane label="登录" name="login">
            <el-form
              :model="loginForm"
              status-icon
              :rules="rules1"
              ref="loginForm"
              label-width="80px"
            >
              <el-form-item label="邮箱" prop="userName">
                <el-input
                  size="small"
                  type="text"
                  v-model="loginForm.userName"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
              
              <el-form-item label="密码" prop="passWord">
                <el-input
                  size="small"
                  type="password"
                  v-model="loginForm.passWord"
                  autocomplete="off"
                ></el-input>
              </el-form-item>

              <el-form-item>
                <el-button
                  size="small"
                  type="primary"
                  @click="submitForm('loginForm')"
                  >提交</el-button
                >
                <el-button size="small" @click="resetForm('loginForm')"
                  >重置</el-button
                >
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <!--  -->
          <el-tab-pane label="注册" name="register">
            <el-form
              :model="ruleForm"
              status-icon
              :rules="rules"
              ref="ruleForm"
              label-width="80px"
            >
              <el-form-item label="邮箱" prop="userName">
                <el-input
                  size="small"
                  type="text"
                  v-model="ruleForm.userName"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
              <el-form-item label="验证码" prop="code">
                <el-input
                  placeholder="请输入内容"
                  v-model="ruleForm.code"
                  size="small"
                >
                  <el-button slot="append" icon="el-icon-search"
                    >发送验证码</el-button
                  >
                </el-input>
              </el-form-item>
              <el-form-item label="密码" prop="passWord">
                <el-input
                  size="small"
                  type="password"
                  v-model="ruleForm.passWord"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
              <el-form-item label="确认密码" prop="checkPass">
                <el-input
                  size="small"
                  type="password"
                  v-model="ruleForm.checkPass"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
              <el-form-item>
                <el-button
                  size="small"
                  type="primary"
                  @click="submitForm('ruleForm')"
                  >提交</el-button
                >
                <el-button size="small" @click="resetForm('ruleForm')"
                  >重置</el-button
                >
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </el-col>
  </el-row>
</template>

<script>
export default {
  data() {
    var userName = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入账号"));
      }
    };
    var code = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入账号"));
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      activeName:'login',
      ruleForm: {
        passWord: "",
        checkPass: "",
        userName: "",
        code:""
      },
      loginForm: {
        passWord: "",
        userName: "",
      },
      rules: {
        userName: [{ validator: userName, trigger: "blur" }],
        passWord: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        code: [{ validator: code, trigger: "blur" }],
      },
      rules1:{
        userName: [{ validator: userName, trigger: "blur" }],
        passWord: [{ validator: validatePass, trigger: "blur" }],
      }
    };
  },
  methods: {
    submitForm(formName) {
      console.log(this.$refs);
      this.$refs[formName].validate((valid) => {
        console.log(valid);
        if (valid) {
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleClick(e){
      console.log(e.name);
    }
  },
};
</script>
<style scoped lang="less">
/deep/.el-form-item__content {
  display: flex;
  justify-content: space-evenly;
}
.buttons {
  width: 100%;
}


</style>